/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.IncomeBillBean;
import com.minierp.dao.IncomeBillDao;
import com.minierp.model.IncomeBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class IncomeBillValidator implements Validator {
	private IncomeBillDao incomeBillDao;

	public boolean supports(Class clazz) {
		return IncomeBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		IncomeBillBean bean = (IncomeBillBean) obj;
		IncomeBill form = bean.getIncomeBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("incomeBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				IncomeBill e = this.incomeBillDao.get(form.getBillCode());
				if (e != null && e.getId() != form.getId()) {
					errors.rejectValue("incomeBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("incomeBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setIncomeDate(sf.parse(bean.getIncomeDateStr()));
			} catch (Exception arg8) {
				hasError = true;
				errors.rejectValue("incomeDateStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getDepartmentId() == 0L) {
				errors.rejectValue("incomeBill.departmentId", "", "请选择『部门』");
				hasError = true;
			} else if (form.getBankAccountId() == 0L) {
				errors.rejectValue("incomeBill.bankAccountId", "", "请选择『收款账户』");
				hasError = true;
			} else if (form.getIncomeTypeId() == 0L) {
				errors.rejectValue("incomeBill.incomeTypeId", "", "请选择『收入类型』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("incomeBill.managerId", "", "请选择『经办人员』");
				hasError = true;
			} else {
				try {
					form.setCost(new BigDecimal(bean.getCostStr()));
				} catch (Exception arg7) {
					hasError = true;
					errors.rejectValue("costStr", "", "『收款金额』只能输入数字");
				}
			}
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 200) {
			hasError = true;
			errors.rejectValue("incomeBill.mark", "", "『摘要』不能超过200个字符");
		}

	}

	public IncomeBillDao getIncomeBillDao() {
		return this.incomeBillDao;
	}

	public void setIncomeBillDao(IncomeBillDao incomeBillDao) {
		this.incomeBillDao = incomeBillDao;
	}
}